import derelict.sdl.sdl;
import Pixels;

import std.stdio: writefln;

public class ImageIO{
	int width;
	int height;
	uint[][] pixels;
	uint background, foreground;
	int positionX, positionY;
	SDL_Surface *surface;
	int scale;
	int bpp = 4;

	this(int h, int w, uint bg, uint fg){
		this.width = w;
		this.height = h;
		pixels.length = w;
		foreach(inout line; pixels)
			line.length = h;
		background = bg;
		foreground = fg;
		fill(background);
	}

	void fill(uint color){
		foreach(int i,inout line; pixels)
			line[] = color;
	}

	void dock(SDL_Surface *surface, int x, int y, int scale){
		this.surface = surface;
		positionX = x;
		positionY = y;
		this.scale = scale;
	}

	void draw(){
		int halfS = scale / 2;
		assert(2*halfS + 1 == scale);
		foreach(int i, line; pixels){
			for(int s = -halfS; s <= halfS; s++){
				uint *pos = cast(uint*)(surface.pixels + (positionY+(i*scale+s)) * surface.pitch + positionX * bpp);
				for(int k = 0; k < line.length; k++){
					pos[-halfS..halfS+1] = line[k];
					pos += scale;
				}
			}
		}
	}

}
